/**
 * Created by litao on 2017/7/14.
 */

var docksUtils = (function () {
    return {
        getAllDocks: function (id) {
            docksUtils.showLoadingImg();
            var format = new OpenLayers.Format.GeoJSON;
            var map = mapObject.getInstance().getMap();
            var pop_len = map.popups.length;
            if (pop_len > 0) {
                for (var i = 0; i < pop_len; i++) {
                    map.removePopup(map.popups[i]);
                }
            }
            map.addLayer(layerUtil.provinceLayer);
            map.addLayer(layerUtil.companyLayer);
            map.addLayer(layerUtil.markersLayer);
            $.ajax({
                url: 'query',
                type: 'post',
                data: {
                    type: 'extent',
                    regionid: id
                },
                dataType: 'json',
                success: function (result) {
                    docksUtils.hideLoadingImg();
                    pierTable.fnClearTable();
                    if (layerUtil.provinceLayer.features.length > 0) {
                        layerUtil.provinceLayer.removeAllFeatures();
                    }
                    if (layerUtil.companyLayer.features.length > 0) {
                        layerUtil.companyLayer.removeAllFeatures();
                    }
                    layerUtil.markersLayer.clearMarkers();
                    var docks = result.data;
                    for (var i = 0; i < docks.length; i++) {
                        var id = docks[i]['xh'];
                        var name = docks[i]['name'];
                        var region = docks[i]['xzqdm'];
                        var type = docks[i]['type'];
                        var owner = docks[i]['owner'];
                        var time = docks[i]['time'];
                        var location = docks[i]['location'];
                        var status = docks[i]['status'];
                        var status_name = docks[i]['status_name'];
                        var detail = docks[i]['detail'];
                        var point = docks[i]['point'];
                        var geo = format.read(point);
                        var info = "";
                        if (status) {
                            info = '具体整治措施： ' + detail.method;
                            info += "<br>" + '是否已完成：  ' + detail.comp_time;
                            info += '<br>' + '下一步工作打算：' + detail.plan;
                        } else {
                            info = '是否已停止运营：' + detail.isStop;
                            info += '<br>' + '是否已拆除：  ' + detail.isRemove;
                            info += '<br>' + '是否已进行复绿：' + detail.isGreen;
                        }

                        var d = "<div  class='tips1 tooltiptopleft' style='text-decoration:underline'>" + status_name + "<div class='tooltip_description' style='display:none' title='详情'>" + info + "</div>" + "</div>";
                        var marker_url = '';
                        var color = 'red';
                        if (status) {
                            marker_url = '../images/markers/marker_green.png';
                            color = 'green';

                        } else {
                            marker_url = '../images/markers/marker.png';
                        }
                        /*
                        var position_x = point['coordinates'][0];
                        var position_y = point['coordinates'][1];

                        var size = new OpenLayers.Size(18, 20);
                        var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
                        var icon = new OpenLayers.Icon(marker_url, size, offset);
                        var marker_fea = new OpenLayers.Marker(new OpenLayers.LonLat(position_x, position_y), icon);

                        layerUtil.markersLayer.addMarker(marker_fea);
                        layerUtil.markersLayer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(position_x, position_y), icon.clone()));
                        */
                        var com_fea = new OpenLayers.Feature.Vector(geo[0]['geometry'], {
                            id: id,
                            name: name,
                            owner: owner,
                            location: location,
                            status_name: status_name,
                            province: region,
                            COLOR: color,
                            FILL: 0.5
                        },
                            { externalGraphic: marker_url, graphicHeight: 25, graphicWidth: 21, graphicXOffset: -12, graphicYOffset: -25 }
                        );
                        //com_fea.createMarker(marker_fea);
                        layerUtil.companyLayer.addFeatures(com_fea);
                        pierTable.fnAddData([id, name, type, owner, time, location, d]);
                        $j(document).ready(function () {
                            $j(".tips1").tooltip();
                        });
                    }
                    //pierTable.fnDraw();
                    var boundary = result.extent['boundary'];
                    var feature = format.read(boundary);
                    layerUtil.provinceLayer.addFeatures(feature);
                    var extent = feature[0].geometry.getBounds();
                    map.zoomToExtent(extent, false);
                }
            })

        },
        /*模糊查询*/
        fuzzySearch: function () {
            var pattern = $('#search_regex').val();
            if (pattern == '' || pattern == undefined || pattern == null) return;
            //$("#search_div").show();
            var format = new OpenLayers.Format.GeoJSON;
            var map = mapObject.getInstance().getMap();
            map.addLayer(layerUtil.companyLayer);
            map.addLayer(layerUtil.markersLayer);
            document.getElementById('search_div').innerHTML = null;
            $.ajax({
                url: 'query',
                type: 'post',
                data: {
                    type: 'name',
                    name: pattern
                },
                dataType: 'json',
                success: function (docks) {
                    pierTable.fnClearTable();
                    if (layerUtil.provinceLayer.features.length > 0) {
                        layerUtil.provinceLayer.removeAllFeatures();
                    }
                    if (layerUtil.companyLayer.features.length > 0) {
                        layerUtil.companyLayer.removeAllFeatures();
                    }
                    layerUtil.markersLayer.clearMarkers();
                    for (var i = 0; i < docks.length; i++) {
                        var id = docks[i]['xh'];
                        var name = docks[i]['name'];
                        var region = docks[i]['xzqdm'];
                        var type = docks[i]['type'];
                        var owner = docks[i]['owner'];
                        var time = docks[i]['time'];
                        var location = docks[i]['location'];
                        var status = docks[i]['status'];
                        var status_name = docks[i]['status_name'];
                        var detail = docks[i]['detail'];
                        var point = docks[i]['point'];
                        var geo = format.read(point);
                        var info = "";
                        if (status) {
                            info = '具体整治措施： ' + detail.method;
                            info += "<br>" + '是否已完成：  ' + detail.comp_time;
                            info += '<br>' + '下一步工作打算：' + detail.plan;
                        } else {
                            info = '是否已停止运营：' + detail.isStop;
                            info += '<br>' + '是否已拆除：  ' + detail.isRemove;
                            info += '<br>' + '是否已进行复绿：' + detail.isGreen;
                        }

                        var d = "<div  class='tips1 tooltiptopleft' style='text-decoration:underline'>" + status_name + "<div class='tooltip_description' style='display:none' title='详情'>" + info + "</div>" + "</div>";
                        var marker_url = '';
                        var color = 'red';
                        if (status) {
                            marker_url = '../images/markers/marker_green.png';
                            color = 'green';

                        } else {
                            marker_url = '../images/markers/marker.png';
                        }
                        /*
                        var position_x = point['coordinates'][0];
                        var position_y = point['coordinates'][1];

                        var size = new OpenLayers.Size(18, 20);
                        var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
                        var icon = new OpenLayers.Icon(marker_url, size, offset);
                        var marker_fea = new OpenLayers.Marker(new OpenLayers.LonLat(position_x, position_y), icon);
                        layerUtil.markersLayer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(position_x, position_y), icon));
                        layerUtil.markersLayer.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(position_x, position_y), icon.clone()));
                        */
                        var com_fea = new OpenLayers.Feature.Vector(geo[0]['geometry'], {
                            id: id,
                            name: name,
                            owner: owner,
                            location: location,
                            status_name: status_name,
                            province: region,
                            COLOR: color,
                            FILL: 0.5,
                        },
                            { externalGraphic: marker_url, graphicHeight: 25, graphicWidth: 21, graphicXOffset: -12, graphicYOffset: -25 }
                        );
                        //com_fea.createMarker(marker_fea);
                        layerUtil.companyLayer.addFeatures(com_fea);
                        pierTable.fnAddData([id, name, type, owner, time, location, d]);
                        $j(document).ready(function () {
                            $j(".tips1").tooltip();
                        });
                    }
                },
                error: function (err) {
                    toastr.error(JSON.stringify(err));
                }
            })
        },
        // /*模糊查询*/
        // fuzzySearch: function () {
        //     var pattern = $('#search_regex').val();
        //     if (pattern == '' || pattern == undefined || pattern == null) return;
        //     $("#search_div").show();
        //     document.getElementById('search_div').innerHTML = null;
        //     $.ajax({
        //         url: 'query/regexSearch/' + pattern,
        //         dataType: 'json',
        //         type: "GET",
        //         success: function (result) {
        //             for (var i = 0; i < result.length; i++) {
        //                 var innerHtml = '<li class="' + result[i].xh + '" onclick="docksUtils.showFuzzyExtent(this)"><a href="javasrripts:void(0)" class="' + result[i].xh + '" onclick="docksUtils.showFuzzyExtent(this)">' + result[i].name + '</a></li>';
        //                 var inner = document.createElement("div");
        //                 inner.innerHTML = innerHtml;
        //                 document.getElementById('search_div').appendChild(inner);
        //             }
        //         },
        //         error: function (err) {
        //             toastr.error(JSON.stringify(err));
        //         }
        //     })
        // },
        /*添加模糊查询结果*/
        showFuzzyExtent: function (obj) {
            $("#search_div").hide();
            var xh = obj.className;
            $.ajax({
                url: 'dock/' + xh,
                type: 'get',
                dataType: 'json',
                error: function (d) {
                    toastr.error('数据库查询出现错误，请重试！');
                },
                success: function (dock) {
                    var id = dock[0]['xh'];
                    var feature = layerUtil.companyLayer.getFeaturesByAttribute('id', id);
                    if (!feature.length) return;
                    var geo = feature[0].geometry.getBounds();
                    geo['left'] = geo['left'] - 0.03;
                    geo['bottom'] = geo['bottom'] - 0.03;
                    geo['right'] = geo['right'] + 0.03;
                    geo['top'] = geo['top'] + 0.03;
                    if (geo) {
                        mapObject.getInstance().getMap().zoomToExtent(geo, false);
                    }
                    layerUtil.selectFeature(feature[0]);
                }
            });
        },
        showLoadingImg: function () {
            var h = $(document).height();
            $(".overlay").css({ "height": h });
            $(".showbox").css({ 'display': 'block' });
            $(".overlay").css({ 'display': 'block', 'opacity': '0.5' });
            // $(".showbox").stop(true).animate({ 'margin-top': '300px', 'opacity': '1' }, 200);
            $(".showbox").css({ 'margin-top': '300px', 'opacity': '1' });
        },
        hideLoadingImg: function () {
            $(".showbox").css({ 'margin-top': '300px', 'opacity': '0' });
            $(".overlay").css({ 'display': 'none', 'opacity': '0' });
            $(".showbox").css({ 'display': 'none' });
        }
    }
})();
